using System;
using System.IO;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.Security;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
/// Summary description for BLL
/// </summary>
public class BLL
{
    public class Default
    {
        private HttpResponse response;
        private HttpRequest request;

        private string strTitle;
        private string strContent;
        private DateTime dtDate_Added;
        private bool bHas_Article;

        public Default()
	    {
		    response = HttpContext.Current.Response;
            request = HttpContext.Current.Request;
	    }

        public void getMenu()
        {
            DAL dal = new DAL();
            DataTable dt = dal.getPages();
            if (dt != null)
            {
                foreach (DataRow  dr in dt.Rows)
                {
                    response.Write("<p ");
                    if (request.QueryString["id"] == null && dr["pages_id"].ToString() == "1")
                    {
                        response.Write("class='active' ");
                        strContent = dr["Content"].ToString();
                        dtDate_Added = DateTime.Parse(dr["Date_Added"].ToString());
                        strTitle = dr["Title"].ToString();
                        if (dr["Has_Article"].ToString() == "Y")
                            bHas_Article = true;
                        else
                            bHas_Article = false;
                    }
                    else if(request.QueryString["id"]!=null && request.QueryString["id"]==dr["pages_id"].ToString())
                    {
                        response.Write("class='active' ");
                        strContent = dr["Content"].ToString();
                        dtDate_Added = DateTime.Parse(dr["Date_Added"].ToString());
                        strTitle = dr["Title"].ToString();
                        if (dr["Has_Article"].ToString() == "Y")
                            bHas_Article = true;
                        else
                            bHas_Article = false;
                    }
                    response.Write("id='page-" + dr["pages_id"].ToString() + "'><a href='default.aspx?id=" + dr["pages_id"].ToString() + "'>" + dr["Menu_title"].ToString() + "</a></p>");
                }
            }
        }

        public void getContent()
        {
            response.Write("<h1>"+strTitle+"</h1>");
            response.Write(strContent);
            response.Write("<p class='toplink'>" + dtDate_Added.ToString("dd MMM yyyy") + " &raquo; <a href='#top'>TOP</a></p><br/><hr/>");
            if (bHas_Article)
                getArticles();
        }

        private void getArticles()
        {
            DAL dal = new DAL();
            DataTable dt = new DataTable();

            if (request.QueryString["id"] == null)
                dt = dal.getArticles("1");
            else
                dt = dal.getArticles(request.QueryString["id"]);

            if (dt != null)
            {
                response.Write("<br><h2>Latest Article &raquo;</h2>");
                response.Write("<h2><a href='articles.aspx?id=" + dt.Rows[0]["articles_id"].ToString() + "'>");
                response.Write(dt.Rows[0]["title"].ToString());
                response.Write("</a></h2>");
                response.Write(DateTime.Parse(dt.Rows[0]["Date_Added"].ToString()).ToString("dd MMM yyyy"));
                response.Write("<blockquote><p>" + dt.Rows[0]["Content"].ToString() + "</p></blockquote>");
                response.Write("<p class='toplink'><a href='#top'>TOP</a></p><br/><hr/>");
            }
            if (dt.Rows.Count > 1)
            {
                response.Write("<h2>Other Articles &raquo;</h2><ul>");
                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    response.Write("<li><a href='articles.aspx?id="+dt.Rows[i]["articles_Id"].ToString()+"'>" + dt.Rows[i]["Title"].ToString() + "</a><br>&raquo;  " + DateTime.Parse(dt.Rows[i]["Date_Added"].ToString()).ToString("dd MMM yyyy") + "</li>");
                    if (i == 5)
                        break;
                }
                response.Write("</ul><br><p class='toplink'><a href='#top'>TOP</a></p>");
            }
        }
    }

    public class Articles
    {
        HttpRequest request=HttpContext.Current.Request;
        HttpResponse response=HttpContext.Current.Response;

        string strPage_Id;
        string strPage_Title;
        string strTitle;
        string strContent;
        DateTime dtDate_Added;

        public Articles()
        {
        }

        public void getArticle()
        {
            response.Write("<h1>" + strTitle + "</h1>");
            response.Write(strContent);
            response.Write("<p class='toplink'>added on &raquo; " + dtDate_Added.ToString("dd MMM yyyy") + " &raquo; <a href='#top'>TOP</a></p><br/><hr/>");
        }

        public void getPage()
        {
            DAL dal = new DAL();
            DataTable dt = new DataTable();
            
            if (request.QueryString["id"] != null)
            {
                dt = dal.getArticle(request.QueryString["id"]);
                if (dt != null && dt.Rows.Count > 0)
                {
                    strPage_Id = dt.Rows[0]["pages_Id"].ToString();
                    strTitle = dt.Rows[0]["title"].ToString();
                    strContent = dt.Rows[0]["content"].ToString();
                    dtDate_Added = DateTime.Parse(dt.Rows[0]["Date_added"].ToString());
                }
            }

            strPage_Title=dal.getPage_Menu_Title(strPage_Id);
            if(strPage_Title=="")
            {
                response.Write("<p class='active'><a href='default.aspx?id=1'>home</a></p>");
            }
            else
            {
                response.Write("<p class='active'><a href='default.aspx?id=" + strPage_Id + "'>" + strPage_Title + "</a></p>");
            }
        }

        public void getArticles()
        {
            DAL dal = new DAL();
            DataTable dt = dal.getArticles(strPage_Id);

            if (dt != null)
            {
                response.Write("<h2>Other Articles</h2><ul>");
                foreach (DataRow dr in dt.Rows)
                {
                    response.Write("<li><a href='articles.aspx?id=" + dr["articles_id"].ToString() + "'>");
                    response.Write(dr["Title"].ToString() + "</a> &raquo; " + DateTime.Parse(dr["date_added"].ToString()).ToString("dd MMM yyyy")+"</li>");
                }
                response.Write("</ul>");
                response.Write("<p class='toplink'><a href='#top'>TOP</a></p>");
            }
        }
    }

    public class Gallery
    {
        private HttpResponse response;
        private HttpRequest request;
        private HttpContext context;

        private string strGallery;
        private string strGalleryCategories;

        private string strTitle;
        private string strContent;
        private DateTime dtDate_Added;
        private bool bHas_Article;

        public Gallery()
	    {
		    response = HttpContext.Current.Response;
            request = HttpContext.Current.Request;
            context = HttpContext.Current;
	    }

        public void getGallery()
        {
            response.Write(strGallery);
        }

        public void getGalleryCategories()
        {
            System.Text.StringBuilder sbGalleryCategories = new System.Text.StringBuilder();

            DirectoryInfo dir = new DirectoryInfo(context.Server.MapPath("img/gallery"));

            DirectoryInfo[] subDirs = dir.GetDirectories();

            if (subDirs.Length > 0)
            {
                for (int count = 0; count < subDirs.Length; count++)
                {
                    sbGalleryCategories.Append("<a href='gallery.aspx?cat=" + subDirs[count].Name + "' >" + subDirs[count].Name + "</a>");
                    if(count!=subDirs.Length-1)
                        sbGalleryCategories.Append(", ");
                }
            }
            else
                sbGalleryCategories.Append("No Gallery found");

            strGalleryCategories = sbGalleryCategories.ToString();

            strGallery = getpGallery();

            response.Write(strGalleryCategories);
        }

        private string getpGallery()
        {
            System.Text.StringBuilder sbGallery = new System.Text.StringBuilder();

            DirectoryInfo maindir = new DirectoryInfo(context.Server.MapPath("img/gallery"));
            
            DirectoryInfo[] subDirs = maindir.GetDirectories();

            if (request.QueryString["cat"] == null)
            {
                if (subDirs.Length > 0)
                {
                    DirectoryInfo dir = subDirs[0];
                    string strCategory = dir.Name;
                    sbGallery.Append("<div><h3>" + strCategory + "</h3></div>");
                    FileInfo[] files = dir.GetFiles("thumb-*.jpg");
                    if (files.Length > 0)
                    {
                        sbGallery.Append("<div class='images'>");
                        foreach (FileInfo file in files)
                        {
                            sbGallery.Append("<div class='image'>");
                            sbGallery.Append("<a href='img/gallery/" + strCategory + "/" + file.Name.Substring(6) + "' rel='lightbox[" + strCategory + "]' title='" + strCategory + " > " + file.Name.Substring(6) + "'>");
                            sbGallery.Append("<img src='img/gallery/" + strCategory + "/" + file.Name + "' alt='" + file.Name + "' />");
                            sbGallery.Append("</a>");
                            sbGallery.Append("</div>");
                        }
                        sbGallery.Append("</div>");
                    }
                    else
                        sbGallery.Append("No Images Found");
                }
                else
                    sbGallery.Append("No galleries found");
                return sbGallery.ToString();
            }
            else
            {
                if (subDirs.Length > 0)
                {
                    DirectoryInfo dir = maindir.GetDirectories(request.QueryString["cat"])[0];
                    string strCategory = dir.Name;
                    sbGallery.Append("<div><h3>" + strCategory + "</h3></div>");
                    FileInfo[] files = dir.GetFiles("thumb-*.jpg");
                    if (files.Length > 0)
                    {
                        sbGallery.Append("<div class='images'>");
                        foreach (FileInfo file in files)
                        {
                            sbGallery.Append("<div class='image'>");
                            sbGallery.Append("<a href='img/gallery/" + strCategory + "/" + file.Name.Substring(6) + "' rel='lightbox[" + strCategory + "]' title=' "+strCategory+" > "+file.Name.Substring(6)+" '>");
                            sbGallery.Append("<img src='img/gallery/" + strCategory + "/" + file.Name + "' alt='" + file.Name + "' />");
                            sbGallery.Append("</a>");
                            sbGallery.Append("</div>");
                        }
                        sbGallery.Append("</div>");
                    }
                    else
                        sbGallery.Append("No Images Found");
                }
                else
                    sbGallery.Append("No galleries found");
                return sbGallery.ToString();
            }
        }

        public void getMenu()
        {
            DAL dal = new DAL();
            DataTable dt = dal.getPages();
            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    response.Write("<p ");
                    if (request.QueryString["id"] == null && dr["pages_id"].ToString() == "1")
                    {
                        response.Write("class='active' ");
                        strContent = dr["Content"].ToString();
                        dtDate_Added = DateTime.Parse(dr["Date_Added"].ToString());
                        strTitle = dr["Title"].ToString();
                        if (dr["Has_Article"].ToString() == "Y")
                            bHas_Article = true;
                        else
                            bHas_Article = false;
                    }
                    else if (request.QueryString["id"] != null && request.QueryString["id"] == dr["pages_id"].ToString())
                    {
                        response.Write("class='active' ");
                        strContent = dr["Content"].ToString();
                        dtDate_Added = DateTime.Parse(dr["Date_Added"].ToString());
                        strTitle = dr["Title"].ToString();
                        if (dr["Has_Article"].ToString() == "Y")
                            bHas_Article = true;
                        else
                            bHas_Article = false;
                    }
                    response.Write("id='page-" + dr["pages_id"].ToString() + "'><a href='default.aspx?id=" + dr["pages_id"].ToString() + "'>" + dr["Menu_title"].ToString() + "</a></p>");
                }
            }
        }
    }

    public class Admin
    {
        private string strPages;
        private string strArticles;
        
        HttpRequest request = HttpContext.Current.Request;
        HttpResponse response = HttpContext.Current.Response;

        public Admin()
        {
        }

        public void getPages()
        {
            System.Text.StringBuilder sbPages = new System.Text.StringBuilder();
            DAL dal = new DAL();
            DataTable dt = dal.getPages();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    sbPages.Append("<div id='page-" + dr["Pages_Id"].ToString() + "' class='element'>");
                    sbPages.Append("<div onclick='loadArticles(" + dr["Pages_Id"].ToString()+");'>" + dr["Menu_Title"].ToString() + "</div>");
                    sbPages.Append("<a href='page.aspx?id=" + dr["Pages_Id"].ToString() + "'>Edit</a>");
                    sbPages.Append("<a href='javascript:void(0);' onclick='deletepage(" + dr["Pages_Id"].ToString() + ");'>Delete</a>");
                    sbPages.Append("</div>");
                }
                strPages = sbPages.ToString();
                response.Write(strPages);
            }
        }

        public void getArticles(string page_Id)
        {
            HttpContext context = HttpContext.Current;
            System.Text.StringBuilder sbArticles = new System.Text.StringBuilder();
            DAL dal = new DAL();
            DataTable dt = dal.getArticles(page_Id);
            if (dt != null)
            {
                sbArticles.Append("<div class='element'>");
                sbArticles.Append("<div><b>Articles</b></div>");
                sbArticles.Append("</div>");
                foreach (DataRow dr in dt.Rows)
                {
                    sbArticles.Append("<div id='article-" + dr["Articles_Id"].ToString() + "' class='element'>");
                    sbArticles.Append("<div>" + dr["Title"].ToString() + "</div>");
                    sbArticles.Append("<a href='article.aspx?id=" + dr["Articles_Id"].ToString() + "'>Edit</a>");
                    sbArticles.Append("<a href='javascript:void(0);' onclick='deleteArticle(" + dr["Articles_Id"].ToString() + ");'>Delete</a>");
                    sbArticles.Append("</div>");
                }
                strArticles = sbArticles.ToString();
                context.Response.Write(strArticles);
            }
            else
                context.Response.Write("No Articles Found...");
        }

        public void getLinks()
        {
            System.Text.StringBuilder sbPages = new System.Text.StringBuilder();
            DAL dal = new DAL();
            DataTable dt = dal.getLinks();
            if (dt.Rows.Count > 0)
            {
                sbPages.Append("<table width='100%'>");
                foreach (DataRow dr in dt.Rows)
                {
                    sbPages.Append("<tr>");
                    sbPages.Append("<td class='linkslist'>" + dr["Title"].ToString() + "</td>");
                    sbPages.Append("<td><a href='links.aspx?id=" + dr["bookmarks_Id"].ToString() + "'>Edit</a></td>");
                    sbPages.Append("<td><a href='javascript:void(0);' onclick='deletelink(" + dr["bookmarks_Id"].ToString() + ")'>Delete</a></td>");
                    sbPages.Append("</tr>");
                }
                sbPages.Append("</table>");
                strPages = sbPages.ToString();
                response.Write(strPages);
            }
        }
    }

    public class Common
    {
        public string searchData(string searchCriteria)
        {
            System.Text.StringBuilder sbSearchRes = new System.Text.StringBuilder();
            DataSet ds = new DataSet();
            DAL dal = new DAL();

            ds = dal.searchData(searchCriteria);
            sbSearchRes.Append("&nbsp;&nbsp;<strong>Search Result</strong>");
            if (ds != null)
            {
                DataTable dtPages = new DataTable();
                DataTable dtArticles = new DataTable();

                dtPages = ds.Tables[0];
                dtArticles = ds.Tables[1];

                if (dtPages.Rows.Count>0)
                {
                    foreach (DataRow dr in dtPages.Rows)
                    {
                        sbSearchRes.Append("<a href='default.aspx?id=" + dr["Pages_Id"].ToString() + "'>&raquo; " +
                            dr["Title"].ToString() + "</a>");
                    }
                }
                
                if (dtArticles.Rows.Count>0)
                {
                    foreach (DataRow dr in dtArticles.Rows)
                    {
                        sbSearchRes.Append("<a href='articles.aspx?id=" + dr["Articles_Id"].ToString() + "'>&raquo; " +
                            dr["Title"].ToString() + "</a>");
                    }
                }

                if (dtArticles.Rows.Count == 0 && dtPages.Rows.Count == 0)
                {
                    sbSearchRes.Append("<br>&nbsp;&nbsp;No Results");
                }
            }
            sbSearchRes.Append("<a href='javascript:void(0);' onclick='CloseSearch();'>&raquo; Close Search &laquo;</a>");
            return sbSearchRes.ToString();
        }

        public void getLinks()
        {
            System.Text.StringBuilder sbLinks = new System.Text.StringBuilder();
            DAL dal = new DAL();

            DataTable dt = dal.getLinks();
            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
	            {
	                sbLinks.Append("<a ");
                    if(dr["Target"].ToString()=="1")
                        sbLinks.Append("target='_blank' ");
                    if(dr["Description"].ToString()!="")
                        sbLinks.Append("title='"+dr["Description"].ToString()+"' ");
                    sbLinks.Append("href='"+dr["URL"].ToString()+"'>"+dr["Title"].ToString()+"</a>");
	            }
                HttpContext.Current.Response.Write(sbLinks.ToString());
            }
            else
            {
                HttpContext.Current.Response.Write("");
            }
        }
    }
}
